package java6681.check;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Junichiro
 * @description 填空题验证窗口
 * @date 2021-03-15 2:24
 **/

public class CheckBlank6681 extends Application {
    private TextArea taTitle;
    private TextField tfInput;
    private TextField tfRegex;
    private Button button;
    private Label lblPrint;

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(getRoot());
        primaryStage.setScene(scene);
        primaryStage.setTitle("检验填空题");
        primaryStage.show();
    }

    private Parent getRoot() {
        /*显示名字学号*/
        Label lblStudent = new Label("212006681|张舒婷");
        /*边框设置*/
        BorderStroke bs = new BorderStroke(Paint.valueOf("#8FBC8F")
                , BorderStrokeStyle.DOTTED
                , new CornerRadii(10)
                , new BorderWidths(3));
        lblStudent.setBorder(new Border(bs));
        lblStudent.setPadding(new Insets(10));

        /*垂直布局*/
        VBox vRadio = new VBox(30);
        /*单选框控件*/
        RadioButton rb1 = new RadioButton("第一题");
        RadioButton rb2 = new RadioButton("第二题");
        RadioButton rb3 = new RadioButton("第三题");
        RadioButton rb4 = new RadioButton("第四题");
        /*创建一个按钮小组*/
        /*设置只能单选，点击单选框显示不同的界面*/
        ToggleGroup tg = new ToggleGroup();
        rb1.setToggleGroup(tg);
        rb2.setToggleGroup(tg);
        rb3.setToggleGroup(tg);
        rb4.setToggleGroup(tg);
        vRadio.getChildren().addAll(rb1, rb2, rb3, rb4);
        vRadio.setAlignment(Pos.CENTER);
        vRadio.setPadding(new Insets(10));

        /*文本框控件*/
        taTitle = new TextArea();
        taTitle.setEditable(false);

        /*水平控件*/
        HBox hInput = new HBox(10);
        Label lblInput = new Label("输入：");
        tfInput = new TextField();
        tfInput.setPromptText("输入被分析的字符串");
        hInput.getChildren().addAll(lblInput, tfInput);
        hInput.setAlignment(Pos.CENTER_LEFT);

        HBox hRegex = new HBox(10);
        Label lblRegex = new Label("正则表达式：");
        tfRegex = new TextField();
        hRegex.getChildren().addAll(lblRegex, tfRegex);
        hRegex.setAlignment(Pos.CENTER_LEFT);

        button = new Button("检验");

        VBox vCenter = new VBox(20);
        vCenter.getChildren().addAll(taTitle, hInput, hRegex, button);
        vCenter.setPadding(new Insets(20.0));
        vCenter.setAlignment(Pos.TOP_CENTER);
        VBox.setVgrow(taTitle, Priority.ALWAYS);
        HBox.setHgrow(tfInput, Priority.ALWAYS);
        HBox.setHgrow(tfRegex, Priority.ALWAYS);

        /*点击按钮后显示*/
        lblPrint = new Label();

        /*BorderPane布局设置控件放置位置*/
        /*布局顶部，底部，左，右或中心区域中的子节点*/
        BorderPane root = new BorderPane();
        root.setTop(lblStudent);
        root.setLeft(vRadio);
        root.setBottom(lblPrint);
        root.setCenter(vCenter);
        root.setPadding(new Insets(10));
        BorderPane.setAlignment(lblStudent, Pos.CENTER);
        BorderPane.setAlignment(lblPrint, Pos.CENTER);

        root.setStyle("-fx-font-size:20");//用CSS样式设置容器中所有节点的字体大小
        rb1.setSelected(true);

        /*点击单选框显示不同的题目*/
        tg.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
                String str1 = "String str = \"2018年成绩：数学83分，英语91分，语文88分，作文80分。\";\n" +
                        "  String regex = \"【1】\" ;\n" +
                        "  String[] words = str.split(regex);\n" +
                        "  System.out.println( 【2】 );";

                String str2 = "String str = \"E:\\\\JavaTest\\\\02Lesson6681\\\\src\";\n" +
                        "String regex =\" 【1】 \";\n" +
                        "String[] dirs = str.split(regex);\n" +
                        "System.out.println( 【2】 );";

                String str3 = "String str = \"第1节String第2节Object第3节Collection\";\n" +
                        "String regex = \"【1】\";\n" +
                        "System.out.println(str.【2】 );";

                String str4 = "String str = \"2816001张达2018年第1学期成绩：数学83.2分，英语91.7分，语文88分，作文80分。\"\n" +
                        "String regex =\" 【1】 \";\n" +
                        "Pattern p = 【2】 ;\n" +
                        "Matcher m = 【3】 ;\n" +
                        "double sum = 0;\n" +
                        "while (m.find()) {\n" +
                        "  sum += Double.parseDouble( str.substring( 【4】 , 【5】) );\n" +
                        "}\n" +
                        "System.out.println(\"sum=\" + sum);";
                /*判断单选按钮点击后显示的相应题目*/
                if (rb1.isSelected()) {
                    taTitle.setText(str1);
                } else if (rb2.isSelected()) {
                    taTitle.setText(str2);
                } else if (rb3.isSelected()) {
                    taTitle.setText(str3);
                } else if (rb4.isSelected()) {
                    taTitle.setText(str4);
                }
            }
        });

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if (tfInput.getText().equals("")||tfRegex.getText().equals("")){
                    lblPrint.setText(" ");
                }
                /*判断选择了哪个题目，调用相应的方法*/
                if (rb1.isSelected() || rb2.isSelected()) {
                    splitRegex();
                } else if (rb3.isSelected()) {
                    replace();
                } else if (rb4.isSelected()) {
                    findRegex();
                }
            }
        });
        return root;
    }

    /*按规则切割字符串，显示*/
    public void splitRegex() {
        String s = tfInput.getText();
        String regex = tfRegex.getText();
        String[] words = s.split(regex);
        lblPrint.setText(Arrays.toString(words));
    }

    /*按规则替换字符串的内容*/
    public void replace() {
        String s = tfInput.getText();
        String regex = tfRegex.getText();
        lblPrint.setText(s.replaceAll(regex, "***"));
    }

    /*按规则查找数字，统计数字总和*/
    public void findRegex() {
        String s = tfInput.getText();
        String regex = tfRegex.getText();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(s);
        double sum = 0;
        while (m.find()) {
            sum += Double.parseDouble(s.substring(m.start(), m.end() - 1));
        }
        lblPrint.setText(String.valueOf(sum));
    }
}
/*
*
\D+

\\\\

[a-zA-Z]+

\d+(\.\d+)?分

* */